package com.power.visual;

import com.power.utils.PowerImage;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Juno Roesler
 */
public class OvalImageButton extends JPanel
{

  private PowerImage image;

  private PowerImage over;

  private boolean isMouseOver = false;

  private boolean isClicked = false;

  private PowerImage clicked;

  private boolean resizable = true;

  private Color overColor, clickColor;

  private List<ActionListener> listeners;

  private static int id = 0;

  private static final String command = "CircleImageButtonCommand";


  public OvalImageButton()
  {
    image = this.createDefaultImage();
    over = null;
    clicked = null;
    listeners = new ArrayList<ActionListener>();
    clickColor = Color.GRAY;
    overColor = new Color(0xF4F4FF); // BRANCO AZULADO CLARO: 244, 244, 230
    
    this.addMouseListener(new MouseAdapter() {
      @Override
      public void mouseEntered(MouseEvent me)
      {
        isMouseOver = true;
        OvalImageButton.this.repaint();
      }
      @Override
      public void mouseExited(MouseEvent me)
      {
        isMouseOver = false;
        OvalImageButton.this.repaint();
      }
      @Override
      public void mousePressed(MouseEvent me)
      {
        isClicked = true;
        OvalImageButton.this.repaint();
      }
      @Override
      public void mouseReleased(MouseEvent me) {
        isClicked = false;
        OvalImageButton.this.repaint();
        fireAction();
      }
    });
  }

  public OvalImageButton(PowerImage img)
  {
    this();
    image = img;
  }

  private void fireAction()
  {
    ActionEvent ae = new ActionEvent(this, id++, command);
    for(ActionListener l : listeners)
      l.actionPerformed(ae);
  }

  public void addActionListener(ActionListener listener)
  {
    if(listener == null) return;
    listeners.add(listener);
  }

  public boolean removeActionListener(ActionListener listener)
  {
    if(listener == null) return false;
    return listeners.remove(listener);
  }

  public PowerImage getOverImage()
  {
    return over;
  }

  public PowerImage getImage()
  {
    return image;
  }

  public PowerImage getClickedImage()
  {
    return clicked;
  }

  public void setImage(PowerImage img)
  {
    image = img;
  }

  public void setOverImage(PowerImage img)
  {
    over = img;
  }

  public void setClickedImage(PowerImage img)
  {
    clicked = img;
  }

  public void setAutoResizeImage(boolean b)
  {
    resizable = b;
  }

  public boolean isAutoResizeImageSet()
  {
    return resizable;
  }

  public void setOverColor(Color c)
  {
    overColor = c;
  }

  public Color getOverColor()
  {
    return overColor;
  }

  public void setClickColor(Color c)
  {
    clickColor = c;
  }

  public Color getClickColor()
  {
    return clickColor;
  }

  @Override
  protected void paintComponent(Graphics g)
  {
    super.paintComponent(g);

    Image img = this.getActualPaintImage();
    if(img == null) return;
    if(resizable)
      img = PowerImage.scale(img, this.getWidth(), this.getHeight());
    
    g.drawImage(img, 0, 0, null);
  }


  private Image getActualPaintImage()
  {
    if(isMouseOver && over == null) {

      Color c = (isClicked ? clickColor : overColor);

      if(c == null) return image.getImage();

      Image img = (isClicked && clicked != null ?
        clicked.getImage() : image.getImage());

      BufferedImage buf = new BufferedImage(this.getWidth(),
          this.getHeight(), BufferedImage.TRANSLUCENT);

      Graphics2D g = (Graphics2D) buf.getGraphics();

      if(resizable)
        img = PowerImage.scale(img, this.getWidth(), this.getHeight());

      g.drawImage(img, 0, 0, null);

      g.setColor(c);

      g.setComposite(AlphaComposite.getInstance(
          AlphaComposite.SRC_OVER, .3f));

      g.fillOval(0, 0, this.getWidth(), this.getHeight());

      return buf;

    } else if(isMouseOver) {
      
      return over.getImage();
      
    } else if(image != null) {

      return image.getImage();
      
    } else {
      
      return null;
      
    }
  }


  private PowerImage createDefaultImage()
  {
    this.setAutoResizeImage(true);

    BufferedImage buf = new BufferedImage(200, 200, BufferedImage.TRANSLUCENT);
    Graphics2D g = (Graphics2D) buf.getGraphics();

    GradientPaint gp = new GradientPaint(
        new Point(0, 0), Color.WHITE,
        new Point(0, 200), Color.BLACK);

    float opacity = .3f;

    g.setColor(new Color(0xE62E2E));

    g.fillOval(0, 0, 200, 200);

    g.setPaint(gp);

    g.setComposite(AlphaComposite.getInstance(
        AlphaComposite.SRC_OVER, opacity));

    g.fillOval(0, 0, 200, 200);

    return new PowerImage(buf);
  }

  
  public static void main(String[] args)
  {
    JFrame f = new JFrame("TestImageButton");
    f.setSize(200, 200);
    f.setLocation(300, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLayout(null);

    final PowerImage down = new PowerImage("/media/warehouse/java/com/power/images/circle-down-arrow-[25].png");
    final PowerImage up = new PowerImage("/media/warehouse/java/com/power/images/circle-up-arrow-[25].png");

    final OvalImageButton ib = new OvalImageButton();
    OvalImageButton ib2 = new OvalImageButton();

    ib.setBounds(10, 10, 27, 27);
    ib.setImage(down);

    ib.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent ae)
      {
        if(ib.getImage() == down)
          ib.setImage(up);
        else
          ib.setImage(down);
      }
    });


    ib2.setBounds(55, 10, 25, 25);

    f.add(ib);
    f.add(ib2);
    f.setVisible(true);
  }

}
